package cn.sinata.wanglin.mine.activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.MySwitchPasswordEditText;

/**
 * 修改密码界面
 */
public class ModifyPasswordActivity extends TitleActivity {

    private String userId;
    @BindView(R.id.spet_old_password)
    EditText spet_old_password;
    @BindView(R.id.spet_new_password)
    EditText spet_new_password;
    @BindView(R.id.spet_conform_password)
    EditText spet_conform_password;
    @Override
    protected int setContentId() {
        return R.layout.activity_modify_password;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("修改密码");
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);

    }

    @OnClick({R.id.tv_forgetPassword,R.id.tv_action})
    void onClick(View v){
        switch (v.getId()){
            case R.id.tv_forgetPassword:
                ActivityUtil.create(mContext).go(ForgetPwdActivity.class).startForResult(0);
                break;
            case R.id.tv_action:
                 String oldPassword=spet_old_password.getText().toString();
                 String newPassword=spet_new_password.getText().toString();
                 String conformPassword=spet_conform_password.getText().toString();
                if (TextUtils.isEmpty(oldPassword)|| oldPassword.length()<6){
                    showToast("请输入至少6位原密码");
                    return;
                }
                if(TextUtils.isEmpty(newPassword)|| newPassword.length()<6){
                    showToast("请输入至少6位新密码");
                    return;
                }
                if (!TextUtils.equals(newPassword,conformPassword)){
                    showToast("两次密码不一致！");
                    return;
                }
                showDialog("修改中...");
                updatePassword(oldPassword,newPassword);
                break;
        }
    }

    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     */
    private void updatePassword(String oldPassword, final String newPassword) {
        HttpManager.alterForUpdatePwd(userId,oldPassword,newPassword).subscribe(new ResultDataSubscriber<JsonObject>(this) {
            @Override
            public void onSuccess(String msg, JsonObject jsonObject) {
                showToast(msg);
                SharedPreferencesUtils.save(Constant.SP_USER_PWD,newPassword);
                setResult(RESULT_OK);
                finish();
            }
        });
    }
}
